/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmPagos.java
 *
 * Created on 10-oct-2011, 9:28:35
 */
package apiformularios.form;

import api.dom.Factura;
import api.dom.Pago;
import api.dom.Utilitaria;
import api.gest.GestFactura;
import api.gest.GestPago;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Luis
 */
public class FrmPagos extends javax.swing.JInternalFrame {

    private JFrame x;

    /** Creates new form FrmPagos */
    /*public FrmPagos() {
    
    initComponents();
    ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
    this.txtNro.setText(String.valueOf(GestPago.getInstance().devolverUltimoNum()));
    Utilitaria.listarPagos((DefaultTableModel) this.jTable2.getModel());
    
    }*/
    public FrmPagos(JFrame x) {

        this.x = x;
        initComponents();
        this.txtNro.setText(String.valueOf(GestPago.getInstance().devolverUltimoNum()));
        ((javax.swing.plaf.basic.BasicInternalFrameUI) this.getUI()).setNorthPane(null);
        Date hoy = new Date();
        fechapago.setDate(hoy);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        txtNro = new javax.swing.JTextField();
        txtNrocli = new javax.swing.JTextField();
        txtSaldoFactura = new javax.swing.JTextField();
        txtImporte = new javax.swing.JTextField();
        fechapago = new com.toedter.calendar.JDateChooser();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        txtNombre1 = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        btnAgregar1 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jLabel7 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        setDoubleBuffered(true);
        setMaximumSize(new java.awt.Dimension(1400, 570));
        setMinimumSize(new java.awt.Dimension(650, 570));
        setPreferredSize(new java.awt.Dimension(650, 588));
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel2.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel2.setText("Fecha de pago :");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 110, -1, -1));

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel3.setText("Cliente :");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(360, 110, 70, -1));

        jLabel5.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel5.setText("Importe pago :");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 140, -1, -1));

        txtNro.setEditable(false);
        getContentPane().add(txtNro, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 80, 190, -1));

        txtNrocli.setEditable(false);
        txtNrocli.setText("0");
        txtNrocli.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtNrocliCaretUpdate(evt);
            }
        });
        getContentPane().add(txtNrocli, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 110, 200, -1));

        txtSaldoFactura.setEditable(false);
        getContentPane().add(txtSaldoFactura, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 80, 200, -1));

        txtImporte.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtImporteKeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtImporteKeyTyped(evt);
            }
        });
        getContentPane().add(txtImporte, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 140, 190, -1));

        fechapago.setToolTipText("");
        fechapago.setOpaque(false);
        getContentPane().add(fechapago, new org.netbeans.lib.awtextra.AbsoluteConstraints(110, 110, 190, -1));

        jPanel2.setBackground(new java.awt.Color(153, 180, 209));
        jPanel2.setPreferredSize(new java.awt.Dimension(580, 71));
        jPanel2.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel6.setBackground(new java.awt.Color(51, 255, 51));
        jLabel6.setFont(new java.awt.Font("Tahoma", 0, 48));
        jLabel6.setText("Pagos");
        jLabel6.setToolTipText("");
        jPanel2.add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 0, -1, -1));

        getContentPane().add(jPanel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 650, 70));

        jPanel3.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        jPanel3.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        txtNombre1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNombre1KeyReleased(evt);
            }
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNombre1KeyTyped(evt);
            }
        });
        jPanel3.add(txtNombre1, new org.netbeans.lib.awtextra.AbsoluteConstraints(130, 10, 100, -1));

        jLabel8.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel8.setText("Buscar por numero: ");
        jPanel3.add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 10, -1, -1));

        getContentPane().add(jPanel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 520, 250, 40));

        btnAgregar1.setText("Agregar");
        btnAgregar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregar1ActionPerformed(evt);
            }
        });
        getContentPane().add(btnAgregar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(560, 520, 80, 40));

        jButton1.setText("Buscar Cliente");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(440, 130, 200, 30));

        jLabel7.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel7.setText("Saldo factura :");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(350, 80, -1, -1));

        jLabel4.setFont(new java.awt.Font("Tahoma", 0, 12));
        jLabel4.setText("N° pago :");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 80, -1, -1));

        jTable2.setAutoCreateRowSorter(true);
        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Numero Factura", "Fecha", "Camion", "Moneda", "Importe", "Saldo"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTable2MouseClicked(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jTable2MouseExited(evt);
            }
        });
        jScrollPane1.setViewportView(jTable2);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 170, 630, 340));

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnAgregar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregar1ActionPerformed
    
        if("".equals(txtNro.getText())){
            JOptionPane.showMessageDialog(rootPane, "Falto ingresar el numero de pago");
        } else if("0".equals(txtNrocli.getText())){
            JOptionPane.showMessageDialog(rootPane, "Falto seleccionar un cliente");
        } else if("".equals(txtImporte.getText())){
            JOptionPane.showMessageDialog(rootPane, "Falto ingresar el importe del pago");
        } else if(fechapago.getDate()==null){
            JOptionPane.showMessageDialog(rootPane, "Falto ingresar la fecha del pago");
        }
        String x1 = this.txtSaldoFactura.getText();
        double u = Double.parseDouble(x1);
        if (u >= 0) {
            Date date = new Date();
            date = (fechapago.getDate());
            DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
            String f = dateFormat.format(date);
            Pago objp = new Pago(Integer.parseInt(txtNro.getText()), f, GestFactura.getInstance().devolverFactura(Integer.parseInt(String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 0)))), Double.parseDouble(txtImporte.getText()));
            GestPago.getInstance().altaPago(objp);
            int x = (int) Integer.parseInt(txtImporte.getText());
            GestFactura.getInstance().modificarSaldoFactura(Integer.parseInt(String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 0))), x);
            JOptionPane.showMessageDialog(rootPane, "Pago Agregado Exitosamente");
            try {
                String d = this.txtNrocli.getText();
                int u2 = Integer.parseInt(d);
                jTable2.setModel(Utilitaria.facturasporclientedebiendo((DefaultTableModel) this.jTable2.getModel(), u2));
            } catch (NumberFormatException e) {
            }
            txtImporte.setText("");
            txtSaldoFactura.setText("");
            // txtNrocli.setText("");
            this.txtNro.setText(String.valueOf(GestPago.getInstance().devolverUltimoNum()));
            //Utilitaria.listarPagos((DefaultTableModel) this.jTable2.getModel());
        } else {
            JOptionPane.showMessageDialog(rootPane, "Imposible realizar pago. El importe deseado es mayor al costo de factura");
        }
    /*} catch (NumberFormatException ex) {
        JOptionPane.showMessageDialog(rootPane, "Falto ingresar algun dato");
    } catch (NullPointerException e) {
        JOptionPane.showMessageDialog(rootPane, "Falto ingresar algun dato");
    }*/

}//GEN-LAST:event_btnAgregar1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        FrmListadoCli F = new FrmListadoCli(this);
        F.setVisible(true);


    }//GEN-LAST:event_jButton1ActionPerformed

    private void txtImporteKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtImporteKeyTyped

        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }
    }//GEN-LAST:event_txtImporteKeyTyped

    private void txtImporteKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtImporteKeyReleased
        // TODO add your handling code here:
        try {
            char caracter = evt.getKeyChar();
            if (((caracter < '0')
                    || (caracter > '9'))
                    && (caracter != evt.VK_BACK_SPACE)) {
                evt.consume();
            }

            String x = this.txtImporte.getText();
            Factura objFact = GestFactura.getInstance().buscarFactura(Integer.parseInt(String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 0))));

            double xx = objFact.getfSaldo();
            double yy = Double.parseDouble(x);
            double xxx = xx - yy;

            this.txtSaldoFactura.setText(String.valueOf(xxx));
        } catch (NumberFormatException e) {
        } catch (NullPointerException a) {
            txtImporte.setText("");
            JOptionPane.showMessageDialog(rootPane, "Debe primero seleccionar una factura");
        }
    }//GEN-LAST:event_txtImporteKeyReleased

    private void txtNrocliCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtNrocliCaretUpdate
        // TODO add your handling code here:
        //this.txtSaldoFactura.setText(String.valueOf(y));
        try {
            String d = this.txtNrocli.getText();
            int u = Integer.parseInt(d);


            jTable2.setModel(Utilitaria.facturasporclientedebiendo((DefaultTableModel) this.jTable2.getModel(), u));
        } catch (NumberFormatException e) {
        }

    }//GEN-LAST:event_txtNrocliCaretUpdate

    private void txtNombre1KeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombre1KeyTyped
        // TODO add your handling code here:
        char caracter = evt.getKeyChar();
        if (((caracter < '0')
                || (caracter > '9'))
                && (caracter != evt.VK_BACK_SPACE)) {
            evt.consume();
        }

    }//GEN-LAST:event_txtNombre1KeyTyped

    private void txtNombre1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombre1KeyReleased
        // TODO add your handling code here:
        try {

            if ("".equals(this.txtNombre1.getText())) {
                this.jTable2.setModel(Utilitaria.facturasporclientedebiendo((DefaultTableModel) jTable2.getModel(), Integer.parseInt(txtNrocli.getText())));
            } else {
                DefaultTableModel modelo32 = (DefaultTableModel) jTable2.getModel();
                modelo32.setRowCount(0);
                Factura objF = GestFactura.getInstance().devolverFactura(Integer.parseInt(txtNombre1.getText()));
                Object[] objFac = {objF.getfNumero(), objF.getfFecha(), objF.getObjCamion().getcMatricula(), objF.getfMoneda(), objF.getfImporte(), objF.getfSaldo()};
                modelo32.addRow(objFac);
            }
        } catch (NumberFormatException e) {
        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(null, "No se encuentra el numero de factura pedido");
            txtNombre1.setText("");
            this.jTable2.setModel(Utilitaria.listarPagos((DefaultTableModel) jTable2.getModel()));
        }
    }//GEN-LAST:event_txtNombre1KeyReleased

private void jTable2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseClicked
    this.txtSaldoFactura.setText("");
    this.txtImporte.setText("");
    if ("0".equals(this.txtNrocli.getText())) {
        } else {
            String x = String.valueOf(jTable2.getValueAt(jTable2.getSelectedRow(), 0));
            int y = Integer.parseInt(x);
            Factura objf = GestFactura.getInstance().devolverFactura(y);
            String saldo = String.valueOf(objf.getfSaldo());
            //double u = Utilitaria.totalDeDiferencia(y);
            //this.txtImporte.setText(String.valueOf(u));
            this.txtSaldoFactura.setText(saldo);
        }
}//GEN-LAST:event_jTable2MouseClicked

private void jTable2MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTable2MouseExited
    // TODO add your handling code here:
    //this.txtNumero.setText(String.valueOf(GestFactura.getInstance().devolverUltimoNum()));
}//GEN-LAST:event_jTable2MouseExited
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar1;
    private com.toedter.calendar.JDateChooser fechapago;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTextField txtImporte;
    private javax.swing.JTextField txtNombre1;
    private javax.swing.JTextField txtNro;
    private javax.swing.JTextField txtNrocli;
    private javax.swing.JTextField txtSaldoFactura;
    // End of variables declaration//GEN-END:variables
}
